package org.hit.burkun.extract;

import java.util.HashMap;
import java.util.Map;

import org.hit.burkun.entities.Entity;
import org.hit.burkun.entities.Entity.TYPE;
import org.hit.burkun.entities.realtions.BaseRealtion;
import org.hit.burkun.extract.FiledsToEntityMapper.FILED;
import org.hit.burkun.obo.dbimport.OboTable;
import org.hit.burkun.obo.dbimport.OboTable.ONTOLOGYNAME;

public class ExtractEntityConfigure {
	private static String ontologyMappingTbName = "ontology_mapping";

	private static FiledsToEntityMapper getOntologyEntityMapper(String tbName,
			Entity.TYPE type) {
		Map<FILED, String> fileds = new HashMap<>();
		fileds.put(FILED.DBID, "acc");
		fileds.put(FILED.DEF, "term_definition");
		fileds.put(FILED.NAME, "name");
		// fileds.put(FILED.UID, "acc"); //注释掉uid
		fileds.put(FILED.PRIMARY_ID, "primary_id");
		// 是否加单引号，dbid为string的时候加
		FiledsToEntityMapper fem = new FiledsToEntityMapper(tbName, type,
				fileds, FILED.PRIMARY_ID, false);
		return fem;
	}

	private static FiledsToRealtionMapper getOntology2OntologyRealtionMapper(
			String tbName, BaseRealtion.TYPE type) {
		Map<org.hit.burkun.extract.FiledsToRealtionMapper.FILED, String> fileds = new HashMap<>();
		if (type == BaseRealtion.TYPE.DO_DO || type == BaseRealtion.TYPE.GO_GO
				|| type == BaseRealtion.TYPE.HP_HP) {
			fileds.put(
					org.hit.burkun.extract.FiledsToRealtionMapper.FILED.ENT1,
					"term1_acc");
			fileds.put(
					org.hit.burkun.extract.FiledsToRealtionMapper.FILED.ENT2,
					"term2_acc");
			fileds.put(
					org.hit.burkun.extract.FiledsToRealtionMapper.FILED.DESC,
					"relationship_type_name");
			FiledsToRealtionMapper frm = new FiledsToRealtionMapper(tbName,
					fileds, type);
			frm.setDirect(true);
			return frm;
		} else {
			fileds.put(
					org.hit.burkun.extract.FiledsToRealtionMapper.FILED.ENT1,
					"term1_acc");
			fileds.put(
					org.hit.burkun.extract.FiledsToRealtionMapper.FILED.ENT2,
					"term2_acc");
			fileds.put(
					org.hit.burkun.extract.FiledsToRealtionMapper.FILED.SCORE,
					"mapping_score");
			fileds.put(
					org.hit.burkun.extract.FiledsToRealtionMapper.FILED.CALC_METHOD,
					"mapping_type");
			FiledsToRealtionMapper frm = new FiledsToRealtionMapper(tbName,
					fileds, type);
			frm.setDirect(false);
			switch (type) {
			case HP_DO: {
				frm.setWherecondition("where ontology1_type = 'HPO' and ontology2_type = 'DO'");
				return frm;
			}
			case DO_GO: {
				frm.setWherecondition("where ontology1_type = 'DO' and ontology2_type = 'GO'");
				return frm;
			}
			case GO_HP: {
				frm.setWherecondition("where ontology1_type = 'GO' and ontology2_type = 'HPO'");
				return frm;
			}
			case GOMF_GOBP: {
				frm.setWherecondition("where ontology1_type = 'GO_MF' and ontology2_type = 'GO_BP'");
				return frm;
			}
			case GOMF_GOCC: {
				frm.setWherecondition("where ontology1_type = 'GO_MF' and ontology2_type = 'GO_CC'");
				return frm;
			}
			case GOBP_GOCC: {
				frm.setWherecondition("where ontology1_type = 'GO_BP' and ontology2_type = 'GO_CC'");
				return frm;
			}
			default:
				return null;
			}
		}
	}

	public static FiledsToEntityMapper getGoEntityMapper() {
		return getOntologyEntityMapper(
				new OboTable(ONTOLOGYNAME.GO).getTermTbName(),
				Entity.TYPE.GENE_ONTOLOGY);
	}

	public static FiledsToEntityMapper getDoEntityMapper() {
		return getOntologyEntityMapper(
				new OboTable(ONTOLOGYNAME.DO).getTermTbName(),
				Entity.TYPE.DISEASE_ONTOLOGY);
	}

	public static FiledsToEntityMapper getHpEntityMapper() {
		return getOntologyEntityMapper(
				new OboTable(ONTOLOGYNAME.HPO).getTermTbName(),
				Entity.TYPE.HP_ONTOLOGY);
	}

	public static FiledsToRealtionMapper getHp2DoRealtionMapper() {
		return getOntology2OntologyRealtionMapper(ontologyMappingTbName,
				BaseRealtion.TYPE.HP_DO);
	}

	public static FiledsToRealtionMapper getDo2GoRealtionMapper() {
		return getOntology2OntologyRealtionMapper(ontologyMappingTbName,
				BaseRealtion.TYPE.DO_GO);
	}

	public static FiledsToRealtionMapper getGo2HpRealtionMapper() {
		return getOntology2OntologyRealtionMapper(ontologyMappingTbName,
				BaseRealtion.TYPE.GO_HP);
	}

	public static FiledsToRealtionMapper getGomf2GobpRealtionMapper() {
		return getOntology2OntologyRealtionMapper(ontologyMappingTbName,
				BaseRealtion.TYPE.GOMF_GOBP);
	}

	public static FiledsToRealtionMapper getGomf2GoccRealtionMapper() {
		return getOntology2OntologyRealtionMapper(ontologyMappingTbName,
				BaseRealtion.TYPE.GOMF_GOCC);
	}

	public static FiledsToRealtionMapper getGobp2GoccRealtionMapper() {
		return getOntology2OntologyRealtionMapper(ontologyMappingTbName,
				BaseRealtion.TYPE.GOBP_GOCC);
	}

	// NCBI 基因ID
	public static FiledsToEntityMapper getGeneEntityMapper() {
		Map<FILED, String> fileds = new HashMap<>();
		fileds.put(FILED.DBID, "gene_id");
		fileds.put(FILED.DEF, "description");
		fileds.put(FILED.NAME, "symbol");
		// fileds.put(FILED.UID, "acc"); //注释掉uid
		fileds.put(FILED.PRIMARY_ID, "primary_id");
		// 是否加单引号，dbid为string的时候加
		FiledsToEntityMapper fem = new FiledsToEntityMapper("gene_term",
				Entity.TYPE.GENE_ID, fileds, FILED.PRIMARY_ID, false);
		return fem;
	}

	// NCBI gene symbol
	public static FiledsToEntityMapper getGeneSymbolSynEntityMapper() {
		Map<FILED, String> fileds = new HashMap<>();
		fileds.put(FILED.DBID, "entrez_gene_id");
		fileds.put(FILED.DEF, "synonyms");
		fileds.put(FILED.NAME, "approved_symbol");
		// fileds.put(FILED.UID, "acc"); //注释掉uid
		fileds.put(FILED.PRIMARY_ID, "primary_id");
		// fileds.put(FILED.UID, "acc"); //注释掉uid
		// fileds.put(FILED.PRIMARY_ID, "primary_id");
		// 是否加单引号，dbid为string的时候加
		// biodata
		FiledsToEntityMapper fem = new FiledsToEntityMapper(
				"gene_term_id_synonym", Entity.TYPE.GENE_SYMBOL, fileds, null,
				false);
		return fem;
	}

	// Ontology syonomys
	public static FiledsToEntityMapper getOntologySynonymEntityMapper(
			Entity.TYPE type) {
		Map<FILED, String> fileds = new HashMap<>();
		// 这里dbid暂时这样搞
		fileds.put(FILED.DBID, "alter_synonym");
		fileds.put(FILED.DEF, "synonym");
		fileds.put(FILED.NAME, "ent_name");
		fileds.put(FILED.OTHER, "synonym_type"); // 注释掉uid
		fileds.put(FILED.PRIMARY_ID, "primary_id");
		// 是否加单引号，dbid为string的时候加
		// biodata
		if (type == TYPE.GENE_ONTOLOGY) {
			FiledsToEntityMapper fem = new FiledsToEntityMapper(
					"go_term_id_synonym", type, fileds, null, false);
			return fem;
		} else if (type == TYPE.DISEASE_ONTOLOGY) {
			FiledsToEntityMapper fem = new FiledsToEntityMapper(
					"do_term_id_synonym", type, fileds, null, false);
			return fem;
		} else if (type == TYPE.HP_ONTOLOGY) {
			FiledsToEntityMapper fem = new FiledsToEntityMapper(
					"hp_term_id_synonym", type, fileds, null, false);
			return fem;
		}
		return null;
	}

	// 考虑了父子关系的Ontology
	public static FiledsToEntityMapper getOntologyParentChildSynonymEntityMapper(
			Entity.TYPE type) {
		Map<FILED, String> fileds = new HashMap<>();
		fileds.put(FILED.DBID, "source1");
		fileds.put(FILED.DEF, "term1_name");
		fileds.put(FILED.NAME, "term2_name");
		fileds.put(FILED.OTHER, "rel_type");
		fileds.put(FILED.PRIMARY_ID, "source2");
		// biodata
		if (type == TYPE.GENE_ONTOLOGY) {
			FiledsToEntityMapper fem = new FiledsToEntityMapper(
					"go_parent_child_term_id_synonym", type, fileds, null,
					false);
			return fem;
		} else if (type == TYPE.DISEASE_ONTOLOGY) {
			FiledsToEntityMapper fem = new FiledsToEntityMapper(
					"do_parent_child_term_id_synonym", type, fileds, null,
					false);
			return fem;
		} else if (type == TYPE.HP_ONTOLOGY) {
			FiledsToEntityMapper fem = new FiledsToEntityMapper(
					"hp_parent_child_term_id_synonym", type, fileds, null,
					false);
			return fem;
		}
		return null;
	}

	public static FiledsToEntityMapper getFakeGeneEntityMapper() {
		return new FiledsToEntityMapper(Entity.TYPE.GENE_ID);
	}

	public static FiledsToEntityMapper getFakeUniportEntityMapper() {
		return new FiledsToEntityMapper(Entity.TYPE.UNIPORT_AC);
	}

}
